package com.example.tsproject.Controller;


import com.example.tsproject.Utils.ThreadLocalUtil;
import com.example.tsproject.entity.*;
import com.example.tsproject.mapper.AssignmentsMapper;
import com.example.tsproject.service.AnswersService;
import com.example.tsproject.service.AssignmentsService;
import com.example.tsproject.service.QuestionsService;
import com.example.tsproject.service.TeachersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teachers")
@Slf4j
public class TeacherController {
    @Autowired
    private TeachersService teachersService;
    @Autowired
    private AssignmentsService assignmentsService;
    @Autowired
    private AnswersService answersService;
    @Autowired
    private QuestionsService questionsService;


    @GetMapping
    public Result<Teachers> getByTNumber(){
        String TNumber= ThreadLocalUtil.get();
        System.out.println(TNumber);
        Teachers teachers=teachersService.findTeachers(TNumber);
        System.out.println(teachers);
        return Result.success(teachers);

    }
    @PostMapping("/update")
    public Result update(@RequestBody Teachers teachers){
        teachersService.updateById(teachers);
        System.out.println("*******************");
        System.out.println(teachers);
        return Result.success();
    }

    @GetMapping("/getAssignmentsList")
    public Result getAssignmentsList(){
        String TNumber=ThreadLocalUtil.get();
        String type ="type1";
        System.out.println(TNumber);
        List<Assignments> list=assignmentsService.getTExamsOrAssignmentsList(TNumber,type);
        System.out.println("----------------");
        System.out.println(list);
        return Result.success(list);
    }
    @GetMapping("/getExamsList")
    public Result getExamsList(){
        String TNumber=ThreadLocalUtil.get();
        String type ="type2";
        System.out.println(TNumber);
        List<Assignments> list=assignmentsService.getTExamsOrAssignmentsList(TNumber,type);
        System.out.println("----------------");
        System.out.println("getExamsList；"+list);
        return Result.success(list);
    }

    @GetMapping("/getStuWorkListByClass")
    public Result getStuWorkListByClass(@RequestParam("classNumber") String classNumber,@RequestParam("name") String name){
        String TNumber=ThreadLocalUtil.get();
        System.out.println(TNumber);
//        TNumber="T1001";
        List<AssignmentsWithStudentName> list=assignmentsService.getStuWorkListWithStudentNameByClass(TNumber,classNumber,name);
        System.out.println("----------------");
        System.out.println(list);
        return Result.success(list);
    }
    @GetMapping("/getAnalysisReport")
    public Result getAnalysisReport(@RequestParam("classNumber") String classNumber,@RequestParam("name") String name){
        String TNumber=ThreadLocalUtil.get();
        System.out.println(TNumber);
//        TNumber="T1001";
        List<Answers> list=answersService.getAnswersListWithStudentNameByClass(TNumber,classNumber,name);

//        for (Answers answer : list) {
//            Integer assignmentQuestionId = answer.getAssignmentQuestionId();
//            Integer assignmentId = answer.getAssignmentId();
//            QuestionAndAssignment question = questionsService.getQuestionByAssignmentQuestionId(assignmentQuestionId,assignmentId);
//            question.getQuestionId();
//            System.out.println(question);}

        // 存储题目内容：questionId -> questionContent（题目ID与内容一一对应）
        Map<Integer, String> questionContents = new HashMap<>();
// 存储统计信息：questionId -> [正确数, 总题数]
        Map<Integer, int[]> stats = new HashMap<>();

        for (Answers answer : list) {
            Integer assignmentQuestionId = answer.getAssignmentQuestionId();
            Integer assignmentId = answer.getAssignmentId();
            QuestionAndAssignment question = questionsService.getQuestionByAssignmentQuestionId(assignmentQuestionId, assignmentId);

            if (question == null) continue; // 跳过无效题目

            Integer questionId = question.getQuestionId();
            String content = question.getQuestionContent();
            boolean isCorrect = "T".equals(question.getTrueorfalse());

            // 存储题目内容（确保唯一）
            questionContents.putIfAbsent(questionId, content);

            // 更新统计信息
            int[] count = stats.computeIfAbsent(questionId, k -> new int[2]);
            count[1]++; // 总题数+1
            if (isCorrect) {
                count[0]++; // 正确数+1
            }
        }

// 生成结果
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map.Entry<Integer, int[]> entry : stats.entrySet()) {
            Integer questionId = entry.getKey();
            int correct = entry.getValue()[0];
            int total = entry.getValue()[1];
            double accuracy = total == 0 ? 0.0 : (correct * 100.0) / total;

            Map<String, Object> item = new HashMap<>();
            item.put("questionContent", questionContents.get(questionId));
            item.put("accuracy", String.format("%.2f%%", accuracy));
            result.add(item);
        }
        System.out.println("result;"+result);


        System.out.println("+++++++++++++++++++++++++++++++++++");
//        System.out.println("结果："+accuracyMap.toString());
        System.out.println("结果："+list);
        return Result.success(result);
    }


}
